
#include <head.h>
#include <sqlite3.h>

// 定义超大 字符列表 因为之前 有报错 500 的长度不够用就定义大点
#define N 50000

/**
 * @description: 
 * @return {*}
 */
int re_print_csv()
{
    printf("执行重定向函数代码执行输出放到csv里面去\n");
    int fd;
    char buff[100] = "z_data_log";
    // buff[strlen(buff) - 2] = '\0';
    strcat(buff, ".md");
    // fd = open(buff, O_WRONLY | O_CREAT | O_TRUNC, 0666);
    fd = open(buff, O_WRONLY | O_APPEND | O_CREAT, 0666);
    sprintf(buff, "%s", __FILE__);
    dup2(fd, 1); // 0 1 2 1 标准输出  0 标准输入  2 标准出错
    printf("***********************一个开始***********************\n");
}

// 用户信息结构体 供  新建用户和修改信息使用
/**
 * @description: 
 * @return {*}
 */
typedef struct user_info
/**
 * @description: 
 * @return {*}
 */
{
    char buff_name[30];
    char buff_age[30];
    char buff_sex[30];
    char buff_passwd[30];
    char buff_salary[30];
    char buff_bumen[30];
    char buff_user_id[30];
} user_t;

// operae 1-登录  2-查询单次  3-退出 4-注册 5-查询历史
// user_name 用户名字
// 密码  (主要用作登录时候验证)
// log_fla 判断用户是否登录
// key_1 要查询的单词
// msg_masse 返回详细信息  登录报错 登录成功 查询单词返回的信息。。。  查询用户历史查询记录的信息。。。  这个是一个比较重要的属性
// user_name_reg 这个只有注册的使用用这个属性   防止用户在注册状态下在注册
// passwd_reg  同上
// reg_fla 返回注册是否成功信息（该值就是 注册函数返回值）  0  成功  -1 失败
/**
 * @description: 
 * @return {*}
 */
typedef struct msg
{
    int operae;
    char user_name[100];
    char passwd[100];
    int log_fla;
    char key_1[100];
    char user_name_reg[100];
    char passwd_reg[100];
    int reg_fla;
    int is_admin;
    user_t user_info;
    char msg_masse[N];
} msg_t;

/**
 * @description: 
 * @param {void} *NotUsed
 * @param {int} argc
 * @param {char} *
 * @param {char} *
 * @return {*}
 */
static int callback(void *NotUsed, int argc, char **argv, char **azColName)
{
    int i;
    for (i = 0; i < argc; i++)
    {
        printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
    }
    printf("\n");
    return 0;
}

// 登录函数  参数解析
// name_text_log  msg_get_user.user_name
// passwd         msg_get_user.passwd
// log_stat       msg_get_user.log_fla  登录状态默认0  为 1 则返回重复登录
// name_text      msg_get_user.user_name
// msg_masse_main msg_masse_main  这个类似于一个中间变量 作为函数里面使用 后面赋值给msg_get_user.msg_masse  函数中间对 返回的消息一直进行修改
// log_in(msg_get_user.user_name, msg_get_user.passwd, msg_get_user.log_fla, msg_get_user.user_name,
//                        msg_masse_main
//    log_in(
// msg_get_user.user_name
// , msg_get_user.passwd,
//  msg_get_user.log_fla,
//   msg_get_user.user_name
//   ,msg_masse_main)
// 当时为什么 弄了这俩一样的函数？？？？ 不知道为啥
// 登录函数返回值解析
// 登录成功时  返回 0  主函数判断并对 消息结构体副fla赋值 1
// 失败时候 返回 -1 主函数 设置 msg_get_user.log_fla 不变 然后
// 返回报文给  用户端
/**
 * @description: 
 * @return {*}
 */
int log_in(
    char *name_text_log,
    char *passwd,
    int log_stat,
    char *name_text,
    char *msg_masse_main,
    int *admin_fla)
{
    if (log_stat == 1)
    {
        printf("您已经登录无需重复登录\n");
        return -1;
    }
    sqlite3 *db;
    int rc;
    char *sql, **dbResult, *errMsg;
    char buff[100] = {0};
    int nRow = 0, nColumn = 0;
    /* Open database */
    rc = sqlite3_open("test.db", &db);
    if (rc)
    {
        fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
        printf(" eeeee 数据库报错无法打开数据库\n");
        exit(0);
    }

    sprintf(buff, "select  passwd from  user_reg where  name_text='%s' ", name_text_log);
    printf("%s", buff);
    sql = buff;
    sqlite3_get_table(db, sql, &dbResult, &nRow, &nColumn, &errMsg);
    if (nRow == 0)
    {
        printf("用户名错误 ::%s  没有这个用户...\n", name_text_log);
        strcpy(msg_masse_main, "用户名错误  没有这个用户");
        sqlite3_close(db);
        return -1;
    }
    int res = strcmp(dbResult[1], passwd);
    if (res == 0)
    {
        strcpy(name_text, name_text_log);
        log_stat = 1;

        printf("log_in success...\n");
        strcpy(msg_masse_main, "log_in success");
        // 登录成功之后 查询一下是不是  管理员用户 is_admin
        // sprintf(buff, "select is_admin  from  user_reg where  name_text='%s' ", name_text_log);
        sprintf(buff, "select is_admin  from  user_reg where  name_text='%s' and is_admin='1' ", name_text_log);
        // sql = buff;
        sql = buff;
        printf("查询管理员buff %s\n", buff);
        printf("查询管理员sql %s\n", sql);
        sqlite3_get_table(db,
                          sql, &dbResult, &nRow, &nColumn, &errMsg);
        // int a=dbResult[1];
        if (nRow == 0)
        {
            printf("该用户不是管理员用户\n");
        }
        else
        {
            printf("该用户就是管理员用户\n");
            *admin_fla = 1;
        }
        // char temp_buff[100] = {0};
        // sprintf(temp_buff, "%-5s\t", dbResult[0]);
        // sprintf(temp_buff, "%-5s\t", dbResult[1]);

        sqlite3_close(db);
        return 0;
    }
    else
    {
        printf("passwd error...\n");
        strcpy(msg_masse_main, "passwd error");

        sqlite3_close(db);
        return -1;
    }
}

// 插入函数  插入消息到表 user_did_his
// 参数解析  name_text 用户名字
//          key_1      查询的内容
// INSERT INTO user_did_his (name_text,key_1) VALUES (name_text,key_1);
/**
 * @description: 
 * @param {char} *name_text
 * @param {int} *key_1
 * @param {int} success_or_not
 * @return {*}
 */
int insert_his(char *name_text, int *key_1, int success_or_not)
{
    printf("insert_his begin \n");
    sqlite3 *db;
    char *zErrMsg = 0;
    int rc;
    char *sql;
    // 这个东西太小了确实不行啊  ？？？  大哥你凑啥热闹啊
    char buff[500] = {0};

    /* Open database */
    rc = sqlite3_open("test.db", &db);
    if (rc)
    {
        fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
        exit(0);
    }

    // INSERT INTO user_did_his (name_text,key_1,explain) VALUES ('新添加','803',(select explain from key_to_explain where key='803'));

    sprintf(buff, "INSERT INTO user_did_his (name_text,key_1,explain,success_or_not) VALUES ('%s','%d',(select explain from key_to_explain where key='%d'),'%d'); ", name_text, key_1, key_1, success_or_not);
    printf("insert_his buffer strlen %zu -- %s\n", strlen(buff), buff);
    sql = buff;
    rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg);
    if (rc != SQLITE_OK)
    {
        fprintf(stderr, "username  register SQL error: \n");
        sqlite3_free(zErrMsg);
        sqlite3_close(db);
        return -1;
    }
    else
    {
        sqlite3_free(zErrMsg);
        sqlite3_close(db);
        return 0;
    }
}

// 查询函数
// user_name       用户名
// key_1           查询的单词
// msg_masse_main  查询返回的详细信息  成功 为 查询到的内容 失败为失败消息
/**
 * @description: 
 * @param {char} *user_name
 * @param {char} *key_1
 * @param {char} *msg_masse_main
 * @return {*}
 */
int do_serch(char *user_name, char *key_1, char *msg_masse_main)
{
    sqlite3 *db;
    int rc;
    char *sql, **dbResult, *errMsg, *buff_p, *buff_p_1;
    char buff[100] = {0};
    int nRow = 0, nColumn = 0;
    /* Open database */
    rc = sqlite3_open("test.db", &db);
    if (rc)
    {
        fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
        exit(0);
    }

    sprintf(buff, "select  trans from  dict_1 where  key_='%s'", key_1);
    sql = buff;
    sqlite3_get_table(db, sql, &dbResult, &nRow, &nColumn, &errMsg);
    if (nRow == 0)
    {
        printf("单词 ::%s  没有这个单词...\n", key_1);
        sprintf(msg_masse_main, "单词 ::%s  没有这个单词...\n", key_1);
        sqlite3_close(db);
        return -1;
    }
    memset(buff, 0, sizeof(buff));
    // 遍历结果 将开始的空格去掉
    for (int i = 0; i < strlen(dbResult[1]); i++)
    {
        if (dbResult[1][i] == 32)
        {
        }
        else
        {
            buff_p = &dbResult[1][i];
            break;
        }
    }

    printf("%s---%s", key_1, buff_p);
    sprintf(msg_masse_main, "%s---%s", key_1, buff_p);
    return 0;
}

// 注册函数
// msg_get_user.reg_fla =
// reg_ins_sql(msg_get_user.user_name_reg, msg_get_user.passwd_reg, msg_masse_main);
// name  msg_get_user.user_name_reg    注册的名字
// msg_get_user.passwd_reg             注册使用的密码
// msg_masse_main                      返回的消息的内容
// 返回值  msg_get_user.reg_fla  = 返回值  默认是0  失败返回-1  失败为用户名已使用

/**
 * @description: 
 * @param {user_t} *user_msg_
 * @param {char} *msg_masse_main
 * @param {user_t} *user_msg_trash
 * @return {*}
 */
int reg_ins_sql(user_t *user_msg_, char *msg_masse_main, user_t *user_msg_trash)
{
    sqlite3 *db;
    char *zErrMsg = 0;
    int rc;
    char *sql;
    char buff[100] = {0};

    /* Open database */
    rc = sqlite3_open("test.db", &db);
    if (rc)
    {
        fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
        exit(0);
    }
    // "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) "
    // "VALUES (5, 'Paul', 32, 'California', 20000.00 ); "

    sprintf(buff, "INSERT INTO user_reg (name_text,passwd) VALUES ('%s','%s'); ", user_msg_trash->buff_name, user_msg_trash->buff_passwd);
    sql = buff;
    rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg);
    printf("决定注册成功的sql  -- %s\n", sql);
    if (rc == SQLITE_OK)
    {
        printf("插入姓名 sql sql 执行成功 \n");
    }
    else
    {
        printf("插入 姓名 sql 执行失败 \n");
        return -1;
    }

    if (user_msg_->buff_age[0] != 10)
    {
        sprintf(buff, "UPDATE user_reg SET age_user = '%s' WHERE name_text = '%s'; ",
                user_msg_trash->buff_age,
                user_msg_trash->buff_name);
        printf("执行sql %s\n", buff);
        // return -1;
        sql = buff;
        rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg);
        if (rc == SQLITE_OK)
            printf("sql 执行成功 \n");
        else
            printf("sql 执行失败 \n");
    }

    if (user_msg_->buff_sex[0] != 10)
    {
        sprintf(buff, "UPDATE user_reg SET sex_user = '%s' WHERE name_text = '%s'; ",
                user_msg_trash->buff_sex,
                user_msg_->buff_name);
        printf("执行sql %s\n", buff);
        // return -1;
        sql = buff;
        rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg);
        if (rc == SQLITE_OK)
            printf("sql 执行成功 \n");
        else
            printf("sql 执行失败 \n");
    }

    if (user_msg_->buff_salary[0] != 10)
    {
        sprintf(buff, "UPDATE user_reg SET salary_user = '%s' WHERE name_text = '%s'; ",
                user_msg_trash->buff_salary,
                user_msg_->buff_name);
        printf("执行sql %s\n", buff);
        // return -1;
        sql = buff;
        rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg);
        if (rc == SQLITE_OK)
            printf("sql 执行成功 \n");
        else
            printf("sql 执行失败 \n");
    }

    if (user_msg_->buff_bumen[0] != 10)
    {
        sprintf(buff, "UPDATE user_reg SET department_user = '%s' WHERE name_text = '%s'; ",
                user_msg_trash->buff_bumen,
                user_msg_->buff_name);
        printf("执行sql %s\n", buff);
        // return -1;
        sql = buff;
        rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg);
        if (rc == SQLITE_OK)
            printf("sql 执行成功 \n");
        else
            printf("sql 执行失败 \n");
    }

    fprintf(stdout, "user ::%s register success\n", user_msg_->buff_name);
    sprintf(msg_masse_main, "user ::%s register success\n", user_msg_->buff_name);
    sqlite3_close(db);
    return 0;
}

// 查询历史操作
// name_text        用户名
// msg_masse_main   返回消息
// select_hsi(msg_get_user.user_name, msg_masse_main);
// 根据用户名字查询历史记录 返回值不做处理
// 执行的sql select  key_1 from  user_did_his where  name_text=name_text
// 结果赋值到 msg_masse_main  里面去
/**
 * @description: 
 * @param {char} *name_text
 * @param {char} *msg_masse_main
 * @return {*}
 */
int select_hsi(char *name_text, char *msg_masse_main)
{
    sqlite3 *db;
    int rc;
    char *sql, **dbResult, *errMsg, *buff_p, *buff_p_1;
    char buff[100] = {0};
    int nRow = 0, nColumn = 0;
    /* Open database */
    rc = sqlite3_open("test.db", &db);
    if (rc)
    {
        fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
        exit(0);
    }
    // sprintf(buff, "select  key_1 from  user_did_his where  name_text='%s'", name_text);
    sprintf(buff, "select * from  user_did_his where  name_text='%s'", name_text);
    sql = buff;
    // sql执行结果 写到 dbResult nRow  nColumn 里面去
    // 	db:数据库
    // sql order:控制语句
    // **dResult:二维数组
    // nRow:查询结果条数
    // nColnm:每条结果包含多少数据
    // errmsg:报错信息
    sqlite3_get_table(db, sql, &dbResult, &nRow, &nColumn, &errMsg);
    if (nRow == 0)
    {
        printf("用户::%s  没有这个查询...\n", name_text);
        sprintf(msg_masse_main, "用户::%s  没有这个查询...\n", name_text);
        sqlite3_close(db);
        return -1;
    }
    char msg_temp[100] = {0};
    printf("用户::%s 共查询了 %d 次\n", name_text, nRow);
    sprintf(msg_masse_main, "用户::%s 共查询了 %d 次\n", name_text, nRow);
    for (int i = 1; i <= nRow; i++)
    {
        memset(msg_temp, 0, 100);
        for (int j = 0; j < nColumn; j++)
        {
            printf("%-5s\t", dbResult[i * nColumn + j]);
            sprintf(msg_temp, "%-5s\t", dbResult[i * nColumn + j]);
            strcat(msg_masse_main, msg_temp);
        }
        // 查看使用量防止越界
        if (0)
        {
            sprintf(msg_temp, "%ld\t", strlen(msg_masse_main));
            strcat(msg_masse_main, msg_temp);
        }
        strcat(msg_masse_main, "\n");
    }
    printf("\n");
    printf("用户::%s 共查询了 %d 次\n", name_text, nRow);
    return 0;
}

// 修改消息 -1 查询用户  user_reg 表的内容 参数就是  用户名字
/**
 * @description: 
 * @param {char} *name_text
 * @param {char} *msg_masse_main
 * @return {*}
 */
int select_user_reg(char *name_text, char *msg_masse_main)
{
    // msg_masse_main
    // strcpy(msg_masse_main,"用户查询 user_reg 表返回的数据\n");
    sqlite3 *db;
    int rc;
    char *sql, **dbResult, *errMsg, *buff_p, *buff_p_1;
    char buff[100] = {0};
    int nRow = 0, nColumn = 0;
    /* Open database */
    rc = sqlite3_open("test.db", &db);
    if (rc)
    {
        fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
        exit(0);
    }

    sprintf(buff, "select  * from  user_reg where  name_text='%s'", name_text);
    sql = buff;
    sqlite3_get_table(db, sql, &dbResult, &nRow, &nColumn, &errMsg);
    if (nRow == 0)
    {
        printf("用户名错误 没有根据用户名查询到数据 \n");
        // sprintf(msg_masse_main, "单词 ::%s  没有这个单词...\n", key_1);
        sqlite3_close(db);
        return -1;
    }
    memset(buff, 0, sizeof(buff));
    // 将结果拿出来放到 消息体里面去
    char msg_temp[100] = {0};
    for (int j = 0; j < nColumn; j++)
    {
        // sprintf(msg_temp, "%-5s\t", dbResult[j]);
        // strcat(msg_masse_main, msg_temp);
        if (j == 0)
        {
            // sprintf(msg_temp, "%-5s\t", dbResult[j]);
            strcat(msg_masse_main, "工号");
        }
        if (j == 1)
        {
            // sprintf(msg_temp, "%-5s\t", dbResult[j]);
            strcat(msg_masse_main, "名字");
        }
        if (j == 2)
        {
            // sprintf(msg_temp, "%-5s\t", dbResult[j]);
            strcat(msg_masse_main, "年龄");
        }
        if (j == 3)
        {
            // sprintf(msg_temp, "%-5s\t", dbResult[j]);
            strcat(msg_masse_main, "性别");
        }
        if (j == 4)
        {
            // sprintf(msg_temp, "%-5s\t", dbResult[j]);
            strcat(msg_masse_main, "工资");
        }
        if (j == 5)
        {
            // sprintf(msg_temp, "%-5s\t", dbResult[j]);
            strcat(msg_masse_main, "部门");
        }
        if (j == 6)
        {
            // sprintf(msg_temp, "%-5s\t", dbResult[j]);
            strcat(msg_masse_main, "密码");
        }
        if (j == 7)
        {
            // sprintf(msg_temp, "%-5s\t", dbResult[j]);
            strcat(msg_masse_main, "用户权限");
        }
        sprintf(msg_temp, "%-5s\n", dbResult[nColumn + j]);
        strcat(msg_masse_main, msg_temp);
    }

    // printf("%s---%s", key_1, buff_p);
    // sprintf(msg_masse_main, "%s---%s", key_1, buff_p);
    // strcpy(msg_masse_main,"用户查询 user_reg表返回的数据\n");
}

// 修改消息 -2 查询用户  user_reg表的内容 参数就是  用户名字
/**
 * @description: 
 * @param {char} *name_text
 * @param {char} *msg_masse_main
 * @return {*}
 */
int select_user_reg_1(char *name_text, char *msg_masse_main)
{
    // msg_masse_main
    strcpy(msg_masse_main, "用户执行修改个人信息 返回的消息\n");
}

// 查询函数  管理员查询函数
/**
 * @description: 
 * @param {char} *msg_masse_main
 * @return {*}
 */
int admin_do_serch(char *msg_masse_main)
{
    sqlite3 *db;
    int rc;
    char *sql, **dbResult, *errMsg, *buff_p, *buff_p_1;
    char buff[100] = {0};
    int nRow = 0, nColumn = 0;
    /* Open database */
    rc = sqlite3_open("test.db", &db);
    if (rc)
    {
        fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
        exit(0);
    }

    sprintf(buff, "select *  from  user_reg ");
    sql = buff;
    sqlite3_get_table(db, sql, &dbResult, &nRow, &nColumn, &errMsg);
    if (nRow == 0)
    {
        sprintf(msg_masse_main, "查询错误数据库报错\n");
        sqlite3_close(db);
        return -1;
    }
    memset(buff, 0, sizeof(buff));
    char msg_temp[100] = {0};
    for (int i = 1; i <= nRow; i++)
    {
        // 各行的具体每个数据
        for (int j = 0; j < nColumn; j++)
        {
            memset(msg_temp, 0, 100);
            printf("%-5s\t", dbResult[i * nColumn + j]);
            sprintf(msg_temp, "%-5s\t", dbResult[i * nColumn + j]);
            strcat(msg_masse_main, msg_temp);
        }
        sprintf(msg_temp, "msg_masse_main 的长度%ld\t", strlen(msg_masse_main));
        // strcat(msg_masse_main, msg_temp);
        strcat(msg_masse_main, "\n");
        printf("msg_temp %s \n", msg_temp);
    }

    // sprintf(msg_masse_main, "--%s", buff_p);
    return 0;
}

/**
 * @description: 
 * @param {char} *msg_masse_main
 * @return {*}
 */
int admin_do_serch_head_10(char *msg_masse_main)
{
    sqlite3 *db;
    int rc;
    char *sql, **dbResult, *errMsg, *buff_p, *buff_p_1;
    char buff[100] = {0};
    int nRow = 0, nColumn = 0;
    /* Open database */
    rc = sqlite3_open("test.db", &db);
    if (rc)
    {
        fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
        exit(0);
    }

    sprintf(buff, "select *  from  user_reg limit 10 ");
    sql = buff;
    sqlite3_get_table(db, sql, &dbResult, &nRow, &nColumn, &errMsg);
    if (nRow == 0)
    {
        sprintf(msg_masse_main, "查询错误数据库报错\n");
        sqlite3_close(db);
        return -1;
    }
    memset(buff, 0, sizeof(buff));
    char msg_temp[100] = {0};
    for (int i = 1; i <= nRow; i++)
    {
        // 各行的具体每个数据
        for (int j = 0; j < nColumn; j++)
        {
            memset(msg_temp, 0, 100);
            printf("%-5s\t", dbResult[i * nColumn + j]);
            sprintf(msg_temp, "%-5s\t", dbResult[i * nColumn + j]);
            strcat(msg_masse_main, msg_temp);
        }
        sprintf(msg_temp, "msg_masse_main 的长度%ld\t", strlen(msg_masse_main));
        // strcat(msg_masse_main, msg_temp);
        strcat(msg_masse_main, "\n");
        printf("msg_temp %s \n", msg_temp);
    }

    // sprintf(msg_masse_main, "--%s", buff_p);
    return 0;
}

// 1- 拿到count 2- 然后 使用offset 取得最后几个内容
/**
 * @description: 
 * @param {char} *msg_masse_main
 * @return {*}
 */
int admin_do_serch_last_10(char *msg_masse_main)
{
    sqlite3 *db;
    int rc;
    char *sql, **dbResult, *errMsg, *buff_p, *buff_p_1;
    char buff[100] = {0};
    int nRow = 0, nColumn = 0;
    /* Open database */
    rc = sqlite3_open("test.db", &db);
    if (rc)
    {
        fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
        exit(0);
    }

    sprintf(buff, "select count(*)  from  user_reg ");
    sql = buff;
    sqlite3_get_table(db, sql, &dbResult, &nRow, &nColumn, &errMsg);
    if (nRow == 0)
    {
        sprintf(msg_masse_main, "查询错误数据库报错\n");
        sqlite3_close(db);
        return -1;
    }
    memset(buff, 0, sizeof(buff));
    char msg_temp[100] = {0};
    for (int i = 1; i <= nRow; i++)
    {
        // 各行的具体每个数据
        for (int j = 0; j < nColumn; j++)
        {
            memset(msg_temp, 0, 100);
            printf("%-5s\t", dbResult[i * nColumn + j]);
            sprintf(msg_temp, "%-5s\t", dbResult[i * nColumn + j]);
            // 这个报错了？？？ 为啥啊
            strcat(msg_masse_main, msg_temp);
        }
        sprintf(msg_temp, "msg_masse_main 的长度%ld\t", strlen(msg_masse_main));
        // strcat(msg_masse_main, msg_temp);
        strcat(msg_masse_main, "\n");
        printf("msg_temp %s \n", msg_temp);
    }
    int index_lasit_10 = atoi(msg_masse_main) - 10;
    // sprintf(msg_masse_main, "--%s", buff_p);
    return index_lasit_10;
}

/**
 * @description: 
 * @param {char} *msg_masse_main
 * @param {int} index_lasit_10
 * @return {*}
 */
int admin_do_serch_last_10_1(char *msg_masse_main, int index_lasit_10)
{
    sqlite3 *db;
    int rc;
    char *sql, **dbResult, *errMsg, *buff_p, *buff_p_1;
    char buff[100] = {0};
    int nRow = 0, nColumn = 0;
    /* Open database */
    rc = sqlite3_open("test.db", &db);
    if (rc)
    {
        fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
        exit(0);
    }
    // select *  from  user_reg limit 10 offset  30;
    sprintf(buff, "select *  from  user_reg   limit 10 offset %d", index_lasit_10);
    sql = buff;
    printf("查询最后几个的sql ==%s \n", sql);
    sqlite3_get_table(db, sql, &dbResult, &nRow, &nColumn, &errMsg);
    if (nRow == 0)
    {
        sprintf(msg_masse_main, "查询错误数据库报错\n");
        sqlite3_close(db);
        return -1;
    }
    memset(buff, 0, sizeof(buff));
    char msg_temp[100] = {0};
    for (int i = 1; i <= nRow; i++)
    {
        // 各行的具体每个数据
        for (int j = 0; j < nColumn; j++)
        {
            memset(msg_temp, 0, 100);
            printf("%-5s\t", dbResult[i * nColumn + j]);
            sprintf(msg_temp, "%-5s\t", dbResult[i * nColumn + j]);
            strcat(msg_masse_main, msg_temp);
        }
        sprintf(msg_temp, "msg_masse_main 的长度%ld\t", strlen(msg_masse_main));
        // strcat(msg_masse_main, msg_temp);
        strcat(msg_masse_main, "\n");
        printf("msg_temp %s \n", msg_temp);
    }

    // sprintf(msg_masse_main, "--%s", buff_p);
    return 0;
}

// 函数入参格式 主要是 字符串 a  10-20  20-100 类型
// first_res  last_res 返回  10 20   20 100  这两组数字
/**
 * @description: 
 * @param {char} *a
 * @param {int} *first_res
 * @param {int} *last_res
 * @return {*}
 */
int get_first_last(char *a, int *first_res, int *last_res)
{
    int henggang;
    for (int i = 0; i < strlen(a); i++)
    {
        if (a[i] == 45)
        {
            henggang = i;
        }
    }

    char buff_tmp[10] = {0};
    strcpy(buff_tmp, a + henggang + 1);
    int last = atoi(buff_tmp);
    a[henggang] = '\0';
    int first = atoi(a);
    *first_res = first;
    *last_res = last;
}

// 定制化查询后续可以增加一个功能就是 先拿到 数据总数 然后给用户端形成一个提示 以避免用户操作   输入的 10-20过大
// 分步骤  1-  在 admin_do_serch_customized 函数里面对用户输入做校验 比如
//             只有3条数据 但是用户输出查找 10-20 你们就返回一个错误给用户端
//
// 2 - 等等
// 入参 msg_masse_main 接收返回值
// index_lasit 10-20 字符串
// 出参  不做校验
/**
 * @description: 
 * @param {char} *msg_masse_main
 * @param {char} *index_lasit
 * @return {*}
 */
int admin_do_serch_customized(char *msg_masse_main, char *index_lasit)
{
    // 解出 两个坐标
    int first_, last_;
    printf("get_first_last(index_lasit, &first_, &last_);\n");
    get_first_last(index_lasit, &first_, &last_);
    printf("解出内容 first_ %d   last_ %d ", first_, last_);
    // return 0;

    sqlite3 *db;
    int rc;
    char *sql, **dbResult, *errMsg, *buff_p, *buff_p_1;
    char buff[100] = {0};
    int nRow = 0, nColumn = 0;
    /* Open database */
    rc = sqlite3_open("test.db", &db);
    if (rc)
    {
        fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
        exit(0);
    }
    // select *  from  user_reg limit 10 offset  30;
    sprintf(buff, "select *  from  user_reg   limit %d offset %d", last_ - first_, first_);
    sql = buff;
    printf("定制化查询sql ==%s \n", sql);
    sqlite3_get_table(db, sql, &dbResult, &nRow, &nColumn, &errMsg);
    if (nRow == 0)
    {
        // if  first_ > 32 给用户返回  查询数据初始位 过大我们只有 count 条数据
        sprintf(msg_masse_main, "查询错误数据库报错\n");
        sqlite3_close(db);
        return -1;
    }
    memset(buff, 0, sizeof(buff));
    char msg_temp[100] = {0};
    for (int i = 1; i <= nRow; i++)
    {
        // 各行的具体每个数据
        for (int j = 0; j < nColumn; j++)
        {
            memset(msg_temp, 0, 100);
            printf("%-5s\t", dbResult[i * nColumn + j]);
            sprintf(msg_temp, "%-5s\t", dbResult[i * nColumn + j]);
            strcat(msg_masse_main, msg_temp);
        }
        sprintf(msg_temp, "msg_masse_main 的长度%ld\t", strlen(msg_masse_main));
        // strcat(msg_masse_main, msg_temp);
        strcat(msg_masse_main, "\n");
        printf("msg_temp %s \n", msg_temp);
    }

    // sprintf(msg_masse_main, "--%s", buff_p);
    return 0;
}

// 管理员查询函数  管理员查询所有用户操作记录函数
/**
 * @description: 
 * @param {char} *msg_masse_main
 * @return {*}
 */
int admin_do_serch_all_his(char *msg_masse_main)
{
    sqlite3 *db;
    int rc;
    char *sql, **dbResult, *errMsg, *buff_p, *buff_p_1;
    char buff[100] = {0};
    int nRow = 0, nColumn = 0;
    /* Open database */
    rc = sqlite3_open("test.db", &db);
    if (rc)
    {
        fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
        exit(0);
    }

    sprintf(buff, "select *  from  user_did_his ");
    sql = buff;
    sqlite3_get_table(db, sql, &dbResult, &nRow, &nColumn, &errMsg);
    if (nRow == 0)
    {
        sprintf(msg_masse_main, "查询错误数据库报错\n");
        sqlite3_close(db);
        return -1;
    }
    memset(buff, 0, sizeof(buff));
    char msg_temp[100] = {0};
    for (int i = 1; i <= nRow; i++)
    {
        // 各行的具体每个数据
        for (int j = 0; j < nColumn; j++)
        {
            memset(msg_temp, 0, 100);
            printf("%-5s\t", dbResult[i * nColumn + j]);
            sprintf(msg_temp, "%-5s\t", dbResult[i * nColumn + j]);
            strcat(msg_masse_main, msg_temp);
        }
        sprintf(msg_temp, "msg_masse_main 的长度%ld\t", strlen(msg_masse_main));
        // strcat(msg_masse_main, msg_temp);
        strcat(msg_masse_main, "\n");
        printf("msg_temp %s \n", msg_temp);
    }

    // sprintf(msg_masse_main, "--%s", buff_p);
    return 0;
}

// 管理员查询函数  管理员查询所有用户操作记录函数
/**
 * @description: 
 * @param {char} *msg_masse_main
 * @return {*}
 */
int admin_do_serch_head_50_his(char *msg_masse_main)
{
    sqlite3 *db;
    int rc;
    char *sql, **dbResult, *errMsg, *buff_p, *buff_p_1;
    char buff[100] = {0};
    int nRow = 0, nColumn = 0;
    /* Open database */
    rc = sqlite3_open("test.db", &db);
    if (rc)
    {
        fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
        exit(0);
    }

    sprintf(buff, "select *  from  user_did_his limit 50");
    sql = buff;
    sqlite3_get_table(db, sql, &dbResult, &nRow, &nColumn, &errMsg);
    if (nRow == 0)
    {
        sprintf(msg_masse_main, "查询错误数据库报错\n");
        sqlite3_close(db);
        return -1;
    }
    memset(buff, 0, sizeof(buff));
    char msg_temp[100] = {0};
    for (int i = 1; i <= nRow; i++)
    {
        // 各行的具体每个数据
        for (int j = 0; j < nColumn; j++)
        {
            memset(msg_temp, 0, 100);
            printf("%-5s\t", dbResult[i * nColumn + j]);
            sprintf(msg_temp, "%-5s\t", dbResult[i * nColumn + j]);
            strcat(msg_masse_main, msg_temp);
        }
        sprintf(msg_temp, "msg_masse_main 的长度%ld\t", strlen(msg_masse_main));
        // strcat(msg_masse_main, msg_temp);
        strcat(msg_masse_main, "\n");
        printf("msg_temp %s \n", msg_temp);
    }

    // sprintf(msg_masse_main, "--%s", buff_p);
    return 0;
}

// 管理员定制化查询用户操作记录
/**
 * @description: 
 * @param {char} *msg_masse_main
 * @param {char} *index_lasit
 * @return {*}
 */
int admin_do_serch_customized_his(char *msg_masse_main, char *index_lasit)
{
    // 解出 两个坐标
    int first_, last_;
    get_first_last(index_lasit, &first_, &last_);
    printf("解出内容 first_ %d   last_ %d ", first_, last_);
    // return 0;

    sqlite3 *db;
    int rc;
    char *sql, **dbResult, *errMsg, *buff_p, *buff_p_1;
    char buff[100] = {0};
    int nRow = 0, nColumn = 0;
    /* Open database */
    rc = sqlite3_open("test.db", &db);
    if (rc)
    {
        fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
        exit(0);
    }
    // select *  from  user_reg limit 10 offset  30;
    sprintf(buff, "select *  from  user_did_his   limit %d offset %d", last_ - first_, first_);
    sql = buff;
    printf("定制化查询sql ==%s \n", sql);
    sqlite3_get_table(db, sql, &dbResult, &nRow, &nColumn, &errMsg);
    if (nRow == 0)
    {
        // if  first_ > 32 给用户返回  查询数据初始位 过大我们只有 count 条数据
        sprintf(msg_masse_main, "查询错误数据库报错\n");
        sqlite3_close(db);
        return -1;
    }
    memset(buff, 0, sizeof(buff));
    char msg_temp[100] = {0};
    for (int i = 1; i <= nRow; i++)
    {
        // 各行的具体每个数据
        for (int j = 0; j < nColumn; j++)
        {
            memset(msg_temp, 0, 100);
            printf("%-5s\t", dbResult[i * nColumn + j]);
            sprintf(msg_temp, "%-5s\t", dbResult[i * nColumn + j]);
            strcat(msg_masse_main, msg_temp);
        }
        sprintf(msg_temp, "msg_masse_main 的长度%ld\t", strlen(msg_masse_main));
        // strcat(msg_masse_main, msg_temp);
        strcat(msg_masse_main, "\n");
        printf("msg_temp %s \n", msg_temp);
    }

    // sprintf(msg_masse_main, "--%s", buff_p);
    return 0;
}

// 翻译发送过来的用户注册信息结构体
/**
 * @description: 
 * @param {user_t} *user_msg_
 * @return {*}
 */
int admin_trans_msg(user_t *user_msg_)
{
    show_info_user_t(*user_msg_);
    printf("admin_trans_msg \n");
    // .buff_name = "aa", // 不用动
    // .buff_age = '\n',  //20
    // .buff_sex = '\n',  // 男
    // .buff_passwd = '\n', // 四位数字
    // .buff_salary='\n',  // 默认0  atoi 解
    // .buff_bumen = '\n', // 默认无部门 1 2 3 对应三个部门
    // .buff_user_id = '\n', // 工号 默认/atoi解
    //  工号单独拿出来 写两个sql就好了  工号就先不管了
    // if (user_msg_->buff_user_id[0] == 10)
    // {

    //     printf("执行了 部门   %d  %s \n", user_msg_->buff_bumen[0], user_msg_->buff_bumen);
    //     strcpy(user_msg_->buff_bumen, "无部门");
    // }

    if (user_msg_->buff_bumen[0] == 10)
    {
        // printf("执行了 部门  1 \n");
        strcpy(user_msg_->buff_bumen, "无部门");
    }
    if (user_msg_->buff_bumen[0] == 49)
    {
        // printf("执行了 部门  2\n");
        strcpy(user_msg_->buff_bumen, "销售部");
    }
    if (user_msg_->buff_bumen[0] == 50)
    {
        // printf("执行了 部门  2\n");

        strcpy(user_msg_->buff_bumen, "技术部");
    }
    if (user_msg_->buff_bumen[0] == 51)
    {
        // printf("执行了 部门  2\n");

        strcpy(user_msg_->buff_bumen, "后勤部");
    }

    if (user_msg_->buff_age[0] == 10)
    {
        strcpy(user_msg_->buff_age, "20");
    }

    if (user_msg_->buff_sex[0] == 10 | user_msg_->buff_sex[0] == 49)
    {
        strcpy(user_msg_->buff_sex, "男");
    }

    if (user_msg_->buff_sex[0] == 50)
    {
        strcpy(user_msg_->buff_sex, "女");
    }

    if (user_msg_->buff_passwd[0] == 10)
    {
        strcpy(user_msg_->buff_passwd, "1111");
    }

    if (user_msg_->buff_salary[0] == 10)
    {
        strcpy(user_msg_->buff_salary, "0");
    }
}

// 管理员注册 -1 失败 0 成功
/**
 * @description: 
 * @param {user_t} *user_msg_
 * @param {char} *msg_masse_main
 * @return {*}
 */
int admin_do_regit_(user_t *user_msg_, char *msg_masse_main)
{
    sqlite3 *db;
    char *zErrMsg = 0;
    int rc;
    char *sql;
    char buff[500] = {0};
    // char name[10];
    /* Open database */
    rc = sqlite3_open("test.db", &db);
    if (rc)
    {
        fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
        exit(0);
    }
    // "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) "
    // "VALUES (5, 'Paul', 32, 'California', 20000.00 ); "

    // sprintf(buff, "INSERT INTO user_reg (name_text,passwd) VALUES ('%s','%s'); ", name, passwd);
    // .buff_name = "aa", // 不用动
    // .buff_age = '\n',  //20
    // .buff_sex = '\n',  // 男
    // .buff_passwd = '\n', // 四位数字
    // .buff_salary='\n',  // 默认0  atoi 解
    // .buff_bumen = '\n', // 默认无部门 1 2 3 对应三个部门
    // .buff_user_id = '\n', // 工号 默认/atoi解
    //  工号单独拿出来 写两个sql就好了  工号就先不管了
    if (user_msg_->buff_user_id[0] == 10)
    {
        sprintf(buff, "INSERT INTO user_reg \
               ( name_text, age_user, sex_user, salary_user, department_user, passwd)VALUES \
                ( '%s', %d, '%s', %d, '%s', '%s') ",
                // atoi(user_msg_->buff_user_id),
                user_msg_->buff_name,
                atoi(user_msg_->buff_age),
                user_msg_->buff_sex,
                atoi(user_msg_->buff_salary),
                user_msg_->buff_bumen,
                user_msg_->buff_passwd);
    }
    else
    {
        sprintf(buff, "INSERT INTO user_reg \
               (name_id, name_text, age_user, sex_user, salary_user, department_user, passwd)VALUES \
                (%d, '%s', %d, '%s', %d, '%s', '%s') ",
                atoi(user_msg_->buff_user_id),
                user_msg_->buff_name,
                atoi(user_msg_->buff_age),
                user_msg_->buff_sex,
                atoi(user_msg_->buff_salary),
                user_msg_->buff_bumen,
                user_msg_->buff_passwd);
    }
    printf("%s\n", buff);
    // return -1;
    sql = buff;
    rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg);

    if (rc != SQLITE_OK)
    {
        fprintf(stderr, "username %s register SQL error: %s\n", user_msg_->buff_name, zErrMsg);
        sprintf(msg_masse_main, "username %s register SQL error: %s\n", user_msg_->buff_name, zErrMsg);
        sqlite3_free(zErrMsg);
        sqlite3_close(db);
        return -1;
    }
    else
    {
        fprintf(stdout, "user ::%s register success\n", user_msg_->buff_name);
        sprintf(msg_masse_main, "user ::%s register success\n", user_msg_->buff_name);
        sqlite3_close(db);
        return 0;
    }
}

// todo 一些具体的操作记录没有返回  只是返回了一个信息  具体修改了那些没有捕获到 后期可以进行修改
// -1 失败 0 成功
/**
 * @description: 
 * @param {user_t} *user_msg_
 * @param {char} *msg_masse_main
 * @param {user_t} *user_msg_trash
 * @return {*}
 */
int admin_update_user_info_(user_t *user_msg_, char *msg_masse_main, user_t *user_msg_trash)
{

    // "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) "
    // "VALUES (5, 'Paul', 32, 'California', 20000.00 ); "

    // sprintf(buff, "INSERT INTO user_reg (name_text,passwd) VALUES ('%s','%s'); ", name, passwd);
    // .buff_name = "aa", // 不用动
    // .buff_age = '\n',  //20
    // .buff_sex = '\n',  // 男
    // .buff_passwd = '\n', // 四位数字
    // .buff_salary='\n',  // 默认0  atoi 解
    // .buff_bumen = '\n', // 默认无部门 1 2 3 对应三个部门
    // .buff_user_id = '\n', // 工号 默认/atoi解
    //  工号单独拿出来 写两个sql就好了  工号就先不管了

    // UPDATE user_reg SET name_text = '2' WHERE name_id = 39;

    // 只能执行一个这个代码 给 rc 赋值
    sqlite3 *db;
    char *zErrMsg = 0;
    int rc;
    char *sql;
    char buff[500] = {0};
    rc = sqlite3_open("test.db", &db);
    if (rc)
    {
        fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
        exit(0);
    }

    sprintf(buff, "select  * from  user_reg  WHERE name_id = '%s'; ",
            user_msg_->buff_user_id);
    printf("执行sql %s\n", buff);
    // return -1;
    sql = buff;
    rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg);
    if (rc == SQLITE_OK)
        printf("sql 执行成功 \n");
    else
        printf("sql 执行失败 \n");

    if (user_msg_->buff_name[0] != 10)
    {
        sprintf(buff, "UPDATE user_reg SET name_text = '%s' WHERE name_id = '%s'; ",
                user_msg_trash->buff_name,
                user_msg_->buff_user_id);
        printf("执行sql %s\n", buff);
        // return -1;
        sql = buff;
        rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg);
        if (rc == SQLITE_OK)
            printf("sql 执行成功 \n");
        else
            printf("sql 执行失败 \n");
    }

    if (user_msg_->buff_age[0] != 10)
    {
        sprintf(buff, "UPDATE user_reg SET age_user = '%s' WHERE name_id = '%s'; ",
                user_msg_trash->buff_age,
                user_msg_->buff_user_id);
        printf("执行sql %s\n", buff);
        // return -1;
        sql = buff;
        rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg);
        if (rc == SQLITE_OK)
            printf("sql 执行成功 \n");
        else
            printf("sql 执行失败 \n");
    }

    if (user_msg_->buff_sex[0] != 10)
    {
        sprintf(buff, "UPDATE user_reg SET sex_user = '%s' WHERE name_id = '%s'; ",
                user_msg_trash->buff_sex,
                user_msg_->buff_user_id);
        printf("执行sql %s\n", buff);
        // return -1;
        sql = buff;
        rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg);
        if (rc == SQLITE_OK)
            printf("sql 执行成功 \n");
        else
            printf("sql 执行失败 \n");
    }

    if (user_msg_->buff_salary[0] != 10)
    {
        sprintf(buff, "UPDATE user_reg SET salary_user = '%s' WHERE name_id = '%s'; ",
                user_msg_trash->buff_salary,
                user_msg_->buff_user_id);
        printf("执行sql %s\n", buff);
        // return -1;
        sql = buff;
        rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg);
        if (rc == SQLITE_OK)
            printf("sql 执行成功 \n");
        else
            printf("sql 执行失败 \n");
    }

    if (user_msg_->buff_bumen[0] != 10)
    {
        sprintf(buff, "UPDATE user_reg SET department_user = '%s' WHERE name_id = '%s'; ",
                user_msg_trash->buff_bumen,
                user_msg_->buff_user_id);
        printf("执行sql %s\n", buff);
        // return -1;
        sql = buff;
        rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg);
        if (rc == SQLITE_OK)
            printf("sql 执行成功 \n");
        else
            printf("sql 执行失败 \n");
    }
    if (user_msg_->buff_passwd[0] != 10)
    {
        sprintf(buff, "UPDATE user_reg SET passwd = '%s' WHERE name_id = '%s'; ",
                user_msg_trash->buff_passwd,
                user_msg_->buff_user_id);
        printf("执行sql %s\n", buff);
        // return -1;
        sql = buff;
        rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg);
        if (rc == SQLITE_OK)
            printf("sql 执行成功 \n");
        else
            printf("sql 执行失败 \n");
    }

    if (rc != SQLITE_OK)
    {
        fprintf(stderr, "username %s register SQL error: %s\n", user_msg_->buff_name, zErrMsg);
        sprintf(msg_masse_main, "username %s register SQL error: %s\n", user_msg_->buff_name, zErrMsg);
        sqlite3_free(zErrMsg);
        sqlite3_close(db);
        return -1;
    }
    else
    {
        fprintf(stdout, "user ::%s register success\n", user_msg_->buff_name);
        sprintf(msg_masse_main, "用户 ::%s 信息修改完毕 \n", user_msg_->buff_name);
        sqlite3_free(zErrMsg);
        sqlite3_close(db);
        return 0;
    }
}

/**
 * @description: 
 * @param {user_t} user_info_msg
 * @return {*}
 */
int show_info_user_t(user_t user_info_msg)
{
    printf(
        "buff_name 【%s】 buff_age 【%s】  buff_sex【%s】  buff_passwd 【%s】 buff_salary【%s】 buff_bumen【%s】 buff_user_id 【%s】\n ",
        user_info_msg.buff_name,
        user_info_msg.buff_age,
        user_info_msg.buff_sex,
        user_info_msg.buff_passwd,
        user_info_msg.buff_salary,
        user_info_msg.buff_bumen,
        user_info_msg.buff_user_id);
}

// 普通用户修改密码 入参 名字 新密码  出参 -1 失败 0 成功
/**
 * @description: 
 * @param {char} *uaer_name
 * @param {char} *passwd
 * @param {char} *msg_masse_main
 * @return {*}
 */
int general_user_change_passwd(char *uaer_name, char *passwd, char *msg_masse_main)
{
    printf(" %s 函数开始执行 :%d\n", __func__, __LINE__);
    printf("%s %s", uaer_name, passwd);

    sqlite3 *db;
    char *zErrMsg = 0;
    int rc;
    char *sql;
    char buff[500] = {0};
    rc = sqlite3_open("test.db", &db);
    if (rc)
    {
        fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
        exit(0);
    }
    sprintf(buff, " UPDATE user_reg SET passwd = '%s' WHERE name_text = '%s'; ",
            passwd, uaer_name);
    printf("执行sql %s\n", buff);
    // return -1;
    sql = buff;
    rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg);

    if (rc == SQLITE_OK)
    {
        printf("sql 执行成功 \n");
        sprintf(msg_masse_main, " %s 密码修改成功 :%s\n", uaer_name, passwd);
        sqlite3_free(zErrMsg);
        sqlite3_close(db);
        return 0;
    }
    else
    {
        printf("sql 执行失败 \n");
        sqlite3_free(zErrMsg);
        sqlite3_close(db);
        return -1;
    }
}

// 普通用户修改   密码 入参 名字 新密码  出参 -1 失败 0 成功
// 执行逻辑 1-  直接根据旧的名字 修改为新的名字
/**
 * @description: 
 * @return {*}
 */
int general_user_change_name_age_sex(
    char *uaer_name,
    user_t *user_msg_,
    char *msg_masse_main)
{

    printf(" %s 函数开始执行 :%d\n", __func__, __LINE__);
    printf("%s %s", uaer_name, "passwd");

    sqlite3 *db;
    char *zErrMsg = 0;
    int rc;
    char *sql;
    char buff[500] = {0};
    rc = sqlite3_open("test.db", &db);
    if (rc)
    {
        fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
        exit(0);
    }
    // UPDATE user_reg SET name_text = '12313' WHERE name_text = 4;
    // UPDATE user_reg SET name_text = '%s' WHERE name_text = '%s' new old ;
    if (user_msg_->buff_name[0] == 10)
    {
        printf("user_msg_->buff_name 为空格 不进行修改  \n");
    }
    else
    {
        sprintf(buff, " UPDATE user_reg SET name_text = '%s' WHERE name_text = '%s'; ",
                user_msg_->buff_name, uaer_name);
        printf("执行sql %s\n", buff);
        // return -1;
        sql = buff;
        rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg);
        if (rc == SQLITE_OK)
        {
            printf("sql 执行成功 \n");
            sprintf(msg_masse_main, " %s 用户名修改成功\n", uaer_name);
            // return 0;
        }
        else
        {
            printf("sql 执行失败 用户名字修改错误  不进行下面的操作了\n");
            return -1;
        }
    }

    if (user_msg_->buff_age[0] != 10)
    {
        sprintf(buff, "UPDATE user_reg SET age_user = '%s' WHERE name_text = '%s'; ",
                user_msg_->buff_age,
                user_msg_->buff_name);
        printf("执行sql %s\n", buff);
        // return -1;
        sql = buff;
        rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg);
        if (rc == SQLITE_OK)
            printf("sql 执行成功 \n");
        else
            printf("sql 执行失败 \n");
    }

    if (user_msg_->buff_sex[0] != 10)
    {
        if (user_msg_->buff_sex[0] == 10 | user_msg_->buff_sex[0] == 49)
        {
            strcpy(user_msg_->buff_sex, "男");
        }

        if (user_msg_->buff_sex[0] == 50)
        {
            strcpy(user_msg_->buff_sex, "女");
        }
        sprintf(buff, "UPDATE user_reg SET sex_user = '%s' WHERE name_text = '%s'; ",
                user_msg_->buff_sex,
                user_msg_->buff_name);
        printf("执行sql %s\n", buff);
        // return -1;
        sql = buff;
        rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg);
        if (rc == SQLITE_OK)
            printf("sql 执行成功 \n");
        else
            printf("sql 执行失败 \n");
    }

    return 0;
}

// 管理员使用工号删除
int adimin_del_by_user_id(char *user_id, char *msg_masse_main)
{
    sqlite3 *db;
    char *zErrMsg = 0;
    int rc;
    char *sql;
    char buff[500] = {0};
    rc = sqlite3_open("test.db", &db);
    if (rc)
    {
        fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
        exit(0);
    }
    // UPDATE user_reg SET name_text = '12313' WHERE name_text = 4;
    // UPDATE user_reg SET name_text = '%s' WHERE name_text = '%s' new old ;

    // DELETE FROM user_reg WHERE name_id = 53;

    sprintf(buff, "DELETE FROM user_reg WHERE name_id = '%s'; ",
            user_id);
    printf("执行sql %s\n", buff);
    sql = buff;
    rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg);
    if (rc == SQLITE_OK)
    {
        printf("sql 执行成功 \n");
        sprintf(msg_masse_main, "删除用户成功 \n");
        sqlite3_free(zErrMsg);
        sqlite3_close(db);
        return 0;
    }
    else
    {
        printf("sql 执行失败 用户名字修改错误  不进行下面的操作了\n");
        sqlite3_free(zErrMsg);
        sqlite3_close(db);
        return -1;
    }
}

// 管理员使用名字删除
int adimin_del_by_user_name(char *user_id, char *msg_masse_main)
{
    sqlite3 *db;
    char *zErrMsg = 0;
    int rc;
    char *sql;
    char buff[500] = {0};
    rc = sqlite3_open("test.db", &db);
    if (rc)
    {
        fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
        exit(0);
    }
    // UPDATE user_reg SET name_text = '12313' WHERE name_text = 4;
    // UPDATE user_reg SET name_text = '%s' WHERE name_text = '%s' new old ;

    // DELETE FROM user_reg WHERE name_id = 53;

    sprintf(buff, "DELETE FROM user_reg WHERE name_text = '%s'; ",
            user_id);
    printf("执行sql %s\n", buff);
    sql = buff;
    rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg);
    if (rc == SQLITE_OK)
    {
        printf("sql 执行成功 \n");
        sprintf(msg_masse_main, "根据用户删除用户成功 \n");
        sqlite3_free(zErrMsg);
        sqlite3_close(db);
        return 0;
    }
    else
    {
        printf("sql 执行失败 用户名字修改错误  不进行下面的操作了\n");
        sqlite3_free(zErrMsg);
        sqlite3_close(db);
        return -1;
    }
}

int main()
{

    // ============ 网络相关的内容===================
    int nfds;
    struct epoll_event ev, events[5];
    struct sockaddr_in clientaddr, serveraddr;
    int epfd, listenfd, maxi, i, sock_fd, conn_fd, n;
    char buff[100] = {0};
    socklen_t clilen;

    epfd = epoll_create(100);
    listenfd = socket(AF_INET, SOCK_STREAM, 0);
    ev.data.fd = listenfd;
    ev.events = EPOLLIN;

    epoll_ctl(epfd, EPOLL_CTL_ADD, listenfd, &ev);
    memset(&serveraddr, 0, sizeof(serveraddr));
    serveraddr.sin_family = AF_INET;
    serveraddr.sin_addr.s_addr = htons(INADDR_ANY);
    serveraddr.sin_port = htons(8888);
    bind(listenfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr));
    listen(listenfd, 5);
    // ============ 网络相关的内容===================
    if (1)
    {
        re_print_csv();
    }
    int quest_i = 0;
    char msg_masse_main[N] = {0};
    msg_t msg_get_user;
    user_t user_info_msg;

    while (1)
    {
        nfds = epoll_wait(epfd, events, 5, 500);
        for (int i = 0; i < nfds; i++)
        {
            if (events[i].data.fd == listenfd)
            {
                clilen = sizeof(struct sockaddr_in);
                conn_fd = accept(listenfd,
                                 (struct sockaddr *)&clientaddr, &clilen);
                printf("accept a new client %s\n", inet_ntoa(clientaddr.sin_addr));
                ev.data.fd = conn_fd;
                ev.events = EPOLLIN;
                epoll_ctl(epfd, EPOLL_CTL_ADD, conn_fd, &ev);
                // 这个是在内核里面写的东西 外面看不到的 只能通过下次执行看到
            }
            else if (events[i].events & EPOLLIN)
            {
                sock_fd = events[i].data.fd;
                if (sock_fd < 0)
                {
                    continue;
                }

                memset(msg_masse_main, 0, N);
                // 阻塞等待 读取用户端发送信息
                memset(&msg_get_user, 0, sizeof(msg_t));

                // n = recv(sock_fd, buff, 100, 0);
                n = read(sock_fd, &msg_get_user, sizeof(msg_t));
                // 这个判断估计进不去
                if (n == 0)
                {
                    close(sock_fd);
                    printf("关闭 %d \n", events[i].data.fd);
                    events[i].data.fd = -1;
                }
                printf("get %d ::%s", events->data.fd, buff);

                ev.data.fd = sock_fd;
                ev.events = EPOLLOUT;
                epoll_ctl(epfd, EPOLL_CTL_MOD, sock_fd, &ev);
            }
            else if (ev.events & EPOLLOUT)
            {
                sock_fd = events[i].data.fd;
                if (sock_fd < 0)
                {
                    continue;
                }

                int acceptfd = sock_fd;
                printf("服务端一次服务开始 服务端收到数据user_name  [%s]operae [%d]log_fla [%d]key_1[%s]passwd[%s] msg_masse [%s]\n",
                       msg_get_user.user_name,
                       msg_get_user.operae,
                       msg_get_user.log_fla,
                       msg_get_user.key_1,
                       msg_get_user.passwd,
                       msg_get_user.msg_masse);

                // 各种操作的逻辑
                quest_i = msg_get_user.operae;
                printf("操作id quest_i::[%d]\n", quest_i);

                // 801 登录
                if (quest_i == 801)
                {
                    printf("登录\n");
                    if (log_in(msg_get_user.user_name, msg_get_user.passwd, msg_get_user.log_fla, msg_get_user.user_name,
                               msg_masse_main, &msg_get_user.is_admin) == 0)
                    {
                        insert_his(msg_get_user.user_name, quest_i, 1);
                        msg_get_user.log_fla = 1;
                    }
                    else
                    {
                        insert_his(msg_get_user.user_name, quest_i, 0);
                    }
                    strcpy(msg_get_user.msg_masse, msg_masse_main);
                    write(acceptfd, &msg_get_user, sizeof(msg_t));
                }

                // 802 普通用户 注册
                if (quest_i == 802)
                {
                    printf("第一个内容 \n");
                    show_info_user_t(msg_get_user.user_info);
                    printf("注册\n");
                    user_t user_old = msg_get_user.user_info;
                    admin_trans_msg(&msg_get_user.user_info);
                    printf("新的内容\n");

                    show_info_user_t(msg_get_user.user_info);
                    printf("旧的内容\n");
                    show_info_user_t(user_old);
                    // admin_update_user_info_(&user_old, msg_masse_main, &msg_get_user.user_info);

                    msg_get_user.reg_fla = reg_ins_sql(&user_old, &msg_masse_main, &msg_get_user.user_info);

                    if (msg_get_user.reg_fla == 0)
                    {
                        insert_his(msg_get_user.user_name, quest_i, 1);
                    }
                    else
                    {
                        insert_his(msg_get_user.user_name, quest_i, 0);
                    }
                    strcpy(msg_get_user.msg_masse, msg_masse_main);
                    write(acceptfd, &msg_get_user, sizeof(msg_t));
                }

                // 803 查询个人信息
                if (quest_i == 803)
                {
                    printf("查询信息\n");
                    select_user_reg(msg_get_user.user_name, msg_masse_main);
                    strcpy(msg_get_user.msg_masse, msg_masse_main);
                    write(acceptfd, &msg_get_user, sizeof(msg_t));
                    insert_his(msg_get_user.user_name, quest_i, 1);
                }

                // 804 普通用户修改密码
                if (quest_i == 804)
                {
                    printf("普通用户修改密码 %d\n", __LINE__);

                    msg_get_user.reg_fla = general_user_change_passwd(msg_get_user.user_name, msg_get_user.user_info.buff_passwd, msg_masse_main);
                    if (msg_get_user.reg_fla == 0)
                    {
                        strcpy(msg_get_user.passwd, msg_get_user.user_info.buff_passwd);
                        printf("密码修改成功修改  当前登录用户的  密码成功 \n");
                    }
                    strcpy(msg_get_user.msg_masse, msg_masse_main);
                    write(acceptfd, &msg_get_user, sizeof(msg_t));
                    insert_his(msg_get_user.user_name, quest_i, 1);
                }

                // 805 普通用户修改 名字 年龄 性别
                if (quest_i == 805)
                {
                    printf(" 普通用户修改 名字 年龄 性别 %d\n", __LINE__);

                    msg_get_user.reg_fla = general_user_change_name_age_sex(msg_get_user.user_name, &msg_get_user.user_info, msg_masse_main);
                    if (msg_get_user.reg_fla == 0)
                    {
                        if (msg_get_user.user_info.buff_name[0] != 10)
                        {
                            printf(" 普通用户修改  名字改为修改的名字 old  [%s] new [%s] \n", msg_get_user.user_name, msg_get_user.user_info.buff_name);
                            strcpy(msg_get_user.user_name, msg_get_user.user_info.buff_name);
                            strcpy(msg_get_user.msg_masse, msg_masse_main);
                            write(acceptfd, &msg_get_user, sizeof(msg_t));
                            insert_his(msg_get_user.user_name, quest_i, 1);
                        }
                        else
                        {
                            strcpy(msg_get_user.msg_masse, "普通用户信息修改成功\n");
                            write(acceptfd, &msg_get_user, sizeof(msg_t));
                            insert_his(msg_get_user.user_name, quest_i, 1);
                        }
                    }
                    else
                    {
                        printf(" 普通用户修改  名字改为修改的名字 old  [%s] new [%s] \n", msg_get_user.user_name, msg_get_user.user_info.buff_name);

                        strcpy(msg_get_user.msg_masse, "修改失败 修改的名字重复了。。。");
                        write(acceptfd, &msg_get_user, sizeof(msg_t));
                        insert_his(msg_get_user.user_name, quest_i, 0);
                    }
                }

                // 806 历史
                if (quest_i == 806)
                {
                    printf("历史");
                    select_hsi(msg_get_user.user_name, msg_masse_main);
                    strcpy(msg_get_user.msg_masse, msg_masse_main);
                    write(acceptfd, &msg_get_user, sizeof(msg_t));
                    insert_his(msg_get_user.user_name, quest_i, 1);
                }

                // 901 管理员查询用户信息 查询所有用户
                if (quest_i == 901)
                {
                    printf("管理员查询用户信息 查询所有用户 ");
                    admin_do_serch(msg_masse_main);
                    strcpy(msg_get_user.msg_masse, msg_masse_main);
                    write(acceptfd, &msg_get_user, sizeof(msg_t));
                    insert_his(msg_get_user.user_name, quest_i, 1);
                }

                // 902 管理员查询前10个用户
                if (quest_i == 902)
                {
                    printf("管理员查询前10个用户 \n");
                    admin_do_serch_head_10(msg_masse_main);
                    strcpy(msg_get_user.msg_masse, msg_masse_main);
                    write(acceptfd, &msg_get_user, sizeof(msg_t));
                    insert_his(msg_get_user.user_name, quest_i, 1);
                }

                // 903 管理员查询最后 10个用户
                if (quest_i == 903)
                {
                    printf("管理员查询最后 10个用户\n");
                    int last_coun = admin_do_serch_last_10(msg_masse_main);
                    memset(msg_masse_main, 0, N);
                    admin_do_serch_last_10_1(msg_masse_main, last_coun);
                    strcpy(msg_get_user.msg_masse, msg_masse_main);
                    write(acceptfd, &msg_get_user, sizeof(msg_t));
                    insert_his(msg_get_user.user_name, quest_i, 1);
                }

                // 904 管理员查询内容定制化查询
                if (quest_i == 904)
                {
                    printf("服务器  服务id 904\n");
                    char buff_tmp[100] = {0};
                    strcpy(buff_tmp, msg_get_user.msg_masse);
                    memset(msg_get_user.msg_masse, 0, N);
                    admin_do_serch_customized(msg_get_user.msg_masse, buff_tmp);
                    // strcpy(msg_get_user.msg_masse, "管理员 查询904 定制化查询");
                    write(acceptfd, &msg_get_user, sizeof(msg_t));
                    insert_his(msg_get_user.user_name, quest_i, 1);
                }

                // 905 管理员查询  所有历史记录
                if (quest_i == 905)
                {
                    printf("管理员查询  所有历史记录");
                    admin_do_serch_all_his(msg_masse_main);
                    strcpy(msg_get_user.msg_masse, msg_masse_main);
                    write(acceptfd, &msg_get_user, sizeof(msg_t));
                    insert_his(msg_get_user.user_name, quest_i, 1);
                }

                // 906 管理员查询 最近50个历史记录
                if (quest_i == 906)
                {
                    printf(" 管理员查询 最近50个历史记录");
                    admin_do_serch_head_50_his(msg_masse_main);
                    strcpy(msg_get_user.msg_masse, msg_masse_main);
                    write(acceptfd, &msg_get_user, sizeof(msg_t));
                    insert_his(msg_get_user.user_name, quest_i, 1);
                }

                // 907 管理员定制化查询历史记录
                if (quest_i == 907)
                {
                    printf("管理员定制化查询历史记录");
                    char buff_tmp[100] = {0};
                    strcpy(buff_tmp, msg_get_user.msg_masse);
                    memset(msg_get_user.msg_masse, 0, N);
                    admin_do_serch_customized_his(msg_masse_main, buff_tmp);
                    strcpy(msg_get_user.msg_masse, msg_masse_main);
                    write(acceptfd, &msg_get_user, sizeof(msg_t));
                    insert_his(msg_get_user.user_name, quest_i, 1);
                }

                // 908 管理员注册用户
                if (quest_i == 908)
                {
                    printf("管理员注册用户");
                    // char buff_tmp[100] = {0};
                    // strcpy(buff_tmp, msg_get_user.msg_masse);
                    // memset(msg_get_user.msg_masse, 0, N);
                    // admin_do_serch_customized_his(msg_masse_main, buff_tmp);
                    admin_trans_msg(&msg_get_user.user_info);
                    int _fla = admin_do_regit_(&msg_get_user.user_info, msg_masse_main);

                    strcpy(msg_get_user.msg_masse, msg_masse_main);
                    write(acceptfd, &msg_get_user, sizeof(msg_t));
                    if (_fla == 0)
                    {
                        insert_his(msg_get_user.user_name, quest_i, 1);
                    }
                    else
                    {
                        insert_his(msg_get_user.user_name, quest_i, 0);
                    }
                }

                // 909 管理员修改用户信息
                if (quest_i == 909)
                {
                    printf("管理员修改用户信息");
                    user_t user_old = msg_get_user.user_info;
                    admin_trans_msg(&msg_get_user.user_info);

                    int _fla = admin_update_user_info_(&user_old, msg_masse_main, &msg_get_user.user_info);

                    strcpy(msg_get_user.msg_masse, msg_masse_main);
                    write(acceptfd, &msg_get_user, sizeof(msg_t));
                    if (_fla == 0)
                    {
                        insert_his(msg_get_user.user_name, quest_i, 1);
                    }
                    else
                    {
                        insert_his(msg_get_user.user_name, quest_i, 0);
                    }
                }

                // 912 管理员删除 使用工号
                if (quest_i == 912)
                {
                    printf("管理员删除 使用工号\n");
                    int _fla = adimin_del_by_user_id(msg_get_user.msg_masse, msg_masse_main);
                    strcpy(msg_get_user.msg_masse, msg_masse_main);
                    write(acceptfd, &msg_get_user, sizeof(msg_t));
                    if (_fla == 0)
                    {
                        insert_his(msg_get_user.user_name, quest_i, 1);
                    }
                    else
                    {
                        insert_his(msg_get_user.user_name, quest_i, 0);
                    }
                }

                // 913 管理员删除 使用名字
                if (quest_i == 913)
                {
                    printf("管理员删除 使用名字\n");
                    int _fla = adimin_del_by_user_name(msg_get_user.msg_masse, msg_masse_main);
                    strcpy(msg_get_user.msg_masse, msg_masse_main);
                    write(acceptfd, &msg_get_user, sizeof(msg_t));
                    if (_fla == 0)
                    {
                        insert_his(msg_get_user.user_name, quest_i, 1);
                    }
                    else
                    {
                        insert_his(msg_get_user.user_name, quest_i, 0);
                    }
                }

                printf("服务端一次服务结束 服务端经过操作返回数据为::user_name[%s]operae [%d]log_fla [%d]key_1[%s]passwd[%s] msg_masse[%s]user_name_reg[%s]passwd_reg[%s] reg_fla[%d]is_admin[%d] \n",
                       msg_get_user.user_name, msg_get_user.operae,
                       msg_get_user.log_fla, msg_get_user.key_1,
                       msg_get_user.passwd,
                       msg_get_user.msg_masse,
                       msg_get_user.user_name_reg,
                       msg_get_user.passwd_reg,
                       msg_get_user.reg_fla,
                       msg_get_user.is_admin);
                // 加上这一句是为了 防止用户断开之后  一直进行操作  用户断开之后就会一直发送 msg_get_user.operae==0 的消息 所以这里做一个拦截
                if (msg_get_user.operae == 0)
                {
                    printf("由于moperae 为0 判断用户断开 服务器也停止\n ");
                    break;
                }

                ev.data.fd = sock_fd;
                ev.events = EPOLLIN;
                epoll_ctl(epfd, EPOLL_CTL_MOD, sock_fd, &ev);
            }
        }
    }
}
